//
//  KLMBaseHomeCell.swift
//  KLM_GN
//
//  Created by Nic on 2025/3/14.
//

import UIKit
import Kingfisher
import SnapKit

class KLMBaseHomeCell: UITableViewCell {

    var homeItem: KLMHomeItem!
    
    var showLine = false {
        didSet {
            line.isHidden = !showLine
        }
    }
    
    let line: UIView = {
        let view = UIView()
        view.backgroundColor = rgba(222, 222, 222, 1)
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupUI()
    }
    func setupUI() {
        selectionStyle = .none
        addSubview(line)
        line.snp.makeConstraints { make in
            make.leading.equalTo(14)
            make.trailing.equalTo(-14)
            make.top.equalToSuperview()
            make.height.equalTo(0.5)
        }
        line.isHidden = true
    }
}


class KLMHomeTitleCell: KLMBaseHomeCell {
    /// 标题标签
    let titleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont(safeName: "PingFangSC-Medium", size: 16)
        label.textColor = rgba(38, 38, 38, 1)
        label.numberOfLines = 0
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()
    let detailButton: KLMBaseButton = {
        let btn = KLMBaseButton()
        btn.configStyle(font: UIFont(safeName: "PingFangSC-Medium", size: 12))
        return btn
    }()
    
    override var homeItem: KLMHomeItem! {
        didSet {
            titleLabel.text = homeItem.title
            titleLabel.textColor = homeItem.titleColor
            
            showLine = homeItem.showLine
            
            if homeItem.type == .delete {
                titleLabel.textAlignment = .center
            } else {
                titleLabel.textAlignment = .left
            }
            
            if homeItem.showArrow {
                detailButton.isHidden = false
                guard let rightTitle = homeItem.rightTitle else {
                    detailButton.setupContent(position: .imageTrailing(spacing: 0), image: homeItem.arrowIcon, title: "")
                    return
                }
                detailButton.setupContent(position: .imageTrailing(spacing: 7), image: homeItem.arrowIcon, title: rightTitle, sizeEqualToContent: true)
            } else {
                detailButton.isHidden = true
                if let rightTitle = homeItem.rightTitle, let rightImage = homeItem.rightImage {
                    detailButton.setupContent(position: .imageTrailing(spacing: 7), image: rightImage, title: rightTitle, sizeEqualToContent: true)
                    detailButton.isHidden = false
                }
                if let rightTitle = homeItem.rightTitle {
                    detailButton.setupContent(position: .imageTrailing(spacing: 0), image: nil, title: rightTitle)
                    detailButton.isHidden = false
                }
                if let rightImage = homeItem.rightImage {
                    detailButton.setupContent(position: .imageTrailing(spacing: 0), image: rightImage, title: "")
                    detailButton.isHidden = false
                }
            }
            detailButton.configStyle(titleColor: homeItem.rightTitleColor)
        }
    }
    
    override func setupUI() {
        super.setupUI()
        addSubview(titleLabel)
        addSubview(detailButton)
        titleLabel.snp.makeConstraints { make in
            make.leading.equalTo(16)
            make.centerY.equalToSuperview()
        }
        
        detailButton.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalTo(-16)
        }
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        if detailButton.isHidden {
            titleLabel.width = width - titleLabel.x * 2
        } else {
            titleLabel.width = detailButton.x - titleLabel.x
        }
    }
}

class KLMHomeMemberCell: KLMBaseHomeCell {
    /// 图标图像视图
    let iconImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        imageView.translatesAutoresizingMaskIntoConstraints = false
        return imageView
    }()
    /// 标题标签
    let titleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont(safeName: "PingFangSC-Medium", size: 16)
        label.textColor = rgba(38, 38, 38, 1)
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()
    /// 子标题标签
    let subTitleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont(safeName: "PingFangSC-Medium", size: 12)
        label.textColor = rgba(129, 129, 129, 1)
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()
    let detailButton: KLMBaseButton = {
        let btn = KLMBaseButton()
        btn.configStyle(font: UIFont(safeName: "PingFangSC-Medium", size: 12))
        return btn
    }()
    
    override var homeItem: KLMHomeItem! {
        didSet {
            titleLabel.text = homeItem.title
            titleLabel.textColor = homeItem.titleColor
            if let icon = homeItem.icon {
                iconImageView.image = UIImage(named: icon)
//                iconImageView.kf.setImage(with: URL(string: icon), placeholder: UIImage(named: "Icon_member"), options: nil) { result in
//                    
//                }
            }
            if let subTitle = homeItem.subTitle {
                subTitleLabel.text = subTitle
            }
            if homeItem.showArrow {
                guard let rightTitle = homeItem.rightTitle else {
                    detailButton.setupContent(position: .imageTrailing(spacing: 0), image: homeItem.arrowIcon, title: "")
                    return
                }
                detailButton.setupContent(position: .imageTrailing(spacing: 7), image: homeItem.arrowIcon, title: rightTitle, sizeEqualToContent: true)
            } else {
                
                if let rightTitle = homeItem.rightTitle, let rightImage = homeItem.rightImage {
                    detailButton.setupContent(position: .imageTrailing(spacing: 7), image: rightImage, title: rightTitle, sizeEqualToContent: true)
                }
                if let rightTitle = homeItem.rightTitle {
                    detailButton.configStyle(with: rightTitle)
                    detailButton.setImage(nil, for: .normal)
                }
                if let rightImage = homeItem.rightImage {
                    detailButton.configStyle(image: rightImage)
                    detailButton.setTitle("", for: .normal)
                }
            }
            detailButton.configStyle(titleColor: homeItem.rightTitleColor)
        }
    }
    
    override func setupUI() {
        super.setupUI()
        addSubview(titleLabel)
        addSubview(subTitleLabel)
        addSubview(iconImageView)
        addSubview(detailButton)
        
        iconImageView.snp.makeConstraints { make in
            make.leading.equalTo(12)
            make.width.height.equalTo(42)
            make.centerY.equalToSuperview()
        }
        
        titleLabel.snp.makeConstraints { make in
            make.leading.equalTo(iconImageView.snp.trailing).offset(9)
            make.top.equalTo(iconImageView.snp.top)
        }
        
        subTitleLabel.snp.makeConstraints { make in
            make.leading.equalTo(titleLabel.snp.leading)
            make.bottom.equalTo(iconImageView.snp.bottom)
        }
        
        detailButton.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalTo(-16)
        }
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        if detailButton.isHidden {
            titleLabel.width = width - titleLabel.x - 12
        } else {
            titleLabel.width = detailButton.x - titleLabel.x
        }
        subTitleLabel.width = titleLabel.width
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
